/* smog_pollution_summary.do: This script generates basic summary statistics of the county-day
panel used for most of the analysis in the paper.  The resulting statistics are reported in 
Table 1*/

clear all


cd ${path}analysis
capture log close
set more off
log using smog_pollution_summary.log, replace

/* This program produces a table of summary statistics for our key variables in the air-quality analysis*/

/*  General conditions: Limit to 1998 to 2009, and exclude counties-years where there aren't 
really inspections (also excluded from regression analysis)*/
local conditions year>=1998 & year<=2009 & inlist(county,5,12,18,22,26,53)==0 & (inlist(county,3,6,11,12,13,17,23,47,58)==0 | (year !=2002&year!=2003))





use pollution_star_merged, replace

foreach pollutant  in co nox ozone{
	replace `pollutant' = `pollutant'*1000
}

/*Declare variables to summarize here*/

local vars co nox ozone pm10 N re_pass

/*Limit to 1998 to 2009, and exclude counties-years where there aren't really inspections (also excluded from regression analysis)*/
local conditions year>=1998 & year<=2009 & inlist(county,5,12,18,22,26,53)==0 & (inlist(county,3,6,11,12,13,17,23,47,58)==0 | (year !=2002&year!=2003))

foreach var of varlist `vars'{

	eststo `var'_all: qui estpost tabstat `var' if `conditions' , by(year)
	eststo `var'_LA: qui estpost tabstat `var' if `conditions' & county == 19, by(year)
	//eststo `var'_SF: qui estpost tabstat `var' if `conditions' & inlist(county,1,7,21,28,38,41,43,48,49), by(year)
}

gen bay_area = inlist(county,1,7,21,28,38,41,43,48,49)

collapse co nox ozone pm10 (sum) N re_pass, by(date bay_area year)

foreach var of varlist `vars'{

	eststo `var'_SF: qui estpost tabstat `var' if year>=1998 & year<=2009 & bay_area, by(year)
}




#delimit ;
esttab *_all using summary.tex, replace not main(mean) booktabs
mtitles("CO (PPB)" "\nox\ (PPB)" "Ozone (PPB)" "PM\$_{10}\$ (\$\mu/m^3\$)" "\# Inspections" "\# Re-Inspections" "Mean \nox\ I/M Reading (PPM)")
noobs nonum nonote nostar postfoot(\midrule ) coeflabels(Total "\midrule Average")
title(Average Daily Air Quality and Smog Check Inspections in California Counties, 1998-2009\label{tab:summary})
mgroups("Panel A: All California", pattern(1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span) substitute(\begin{tabular} \resizebox{\textwidth}{!}{\begin{tabular})

;

#delimit ;
esttab *_LA using summary.tex, append not main(mean) booktabs
mtitles("CO (PPB)" "\nox\ (PPB)" "Ozone (PPB)" "PM\$_{10}\$ (\$\mu/m^3\$)" "\# Inspections" "\# Re-Inspections" "Mean \nox\ I/M Reading (PPM)")
noobs nonum nonote nostar prehead( ) postfoot(\midrule)  coeflabels(Total "\midrule Average")
title(Average Daily Air Quality and Smog Check Inspections in California by Year, 1998-2009\label{tab:summary})
mgroups("Panel B: Los Angeles County", pattern(1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span)

;

#delimit ;
esttab *_SF using summary.tex, append not main(mean) booktabs
mtitles("CO (PPB)" "\nox\ (PPB)" "Ozone (PPB)" "PM\$_{10}\$ (\$\mu/m^3\$)" "\# Inspections" "\# Re-Inspections" "Mean \nox\ I/M Reading (PPM)")
noobs nonum nonote nostar prehead( )  coeflabels(Total "\midrule Average")
title(Average Daily Air Quality and Smog Check Inspections in California by Year, 1998-2009\label{tab:summary})
mgroups("Panel C: San Francisco Bay Area", pattern(1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span)
addnote(Note: Excludes counties and years where biennial inspections are not required) substitute(\end{tabular} \end{tabular}})
;
